#pragma once
#include ".\d3dsetting.h"
#include "Sys.h"

LPDIRECT3DDEVICE9				g_pd3dDevice	= NULL;

CD3DSetting::CD3DSetting(void)
{
	SAFE_ZERO( m_pD3D );
}

CD3DSetting::~CD3DSetting(void)
{
}

bool CD3DSetting::Init()
{
	if( ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) == NULL )
	{
		OutputDebugString( "D3D Create failed! , CD3DSetting Init() \n" );
		return FALSE;
	}

	D3DDISPLAYMODE	d3ddm;
	if( FAILED( m_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT , &d3ddm ) ) )
	{
		OutputDebugString( "GetAdapterDisplayMode failed! , D3DSetting" );
		return FALSE;
	}

	if( FAILED( m_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , &m_d3dcaps ) ) )
	{
		OutputDebugString( "GetDeviceCaps failed! , D3DSetting" );
		return FALSE;
	}

	DWORD dwCaps;
	dwCaps = ( m_d3dcaps.VertexProcessingCaps != 0 ) ? D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	D3DPRESENT_PARAMETERS	d3dpp;
	ZeroMemory( &d3dpp , sizeof( d3dpp ) );

	d3dpp.Windowed							= TRUE;

	if( d3dpp.Windowed	== TRUE )
	{
		d3dpp.BackBufferWidth				= MAX_CLIENT_X;
		d3dpp.BackBufferHeight				= MAX_CLIENT_Y;
        d3dpp.BackBufferFormat				= d3ddm.Format;
		d3dpp.SwapEffect					= D3DSWAPEFFECT_DISCARD;
		d3dpp.PresentationInterval			= D3DPRESENT_INTERVAL_IMMEDIATE;
		d3dpp.EnableAutoDepthStencil		= TRUE;
		d3dpp.AutoDepthStencilFormat		= D3DFMT_D16;
	}
	else
	{
		d3dpp.BackBufferWidth				= MAX_CLIENT_X;
		d3dpp.BackBufferHeight				= MAX_CLIENT_Y;
        d3dpp.BackBufferFormat				= d3ddm.Format;
		d3dpp.SwapEffect					= D3DSWAPEFFECT_DISCARD;
		d3dpp.PresentationInterval			= D3DPRESENT_INTERVAL_IMMEDIATE;
		d3dpp.EnableAutoDepthStencil		= TRUE;
		d3dpp.AutoDepthStencilFormat		= D3DFMT_D16;		
		d3dpp.FullScreen_RefreshRateInHz	= D3DPRESENT_RATE_DEFAULT;
		d3dpp.Flags							= 0;
	}
	

	if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , g_Engine.m_hWnd , 
				dwCaps , &d3dpp , &g_pd3dDevice ) ) )
	{
		OutputDebugString( "Create device failed! , CD3DSetting Init() \n" );
		return FALSE;
	}

	return TRUE;
}

bool CD3DSetting::Release()
{
	SAFE_RELEASE( g_pd3dDevice );
	SAFE_RELEASE( m_pD3D );	

	return TRUE;
}